package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationGetSNsByPackageBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationGetSNsByPackageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓位列表
 *
 * @author 詹建宇
 * @time on 2018/4/8 17:43
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInPositionsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private EditText editText;//搜索仓位
    private ImageView search;
    private LinearLayout positionLayout;

    private String mat_id;
    private String batch;

    AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position> positionBeanList = new ArrayList<>();

    private int id;//动态添加的仓位view的id

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_positions);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        editText = (EditText) activity.findViewById(R.id.editText);
        search = (ImageView) activity.findViewById(R.id.search);
        positionLayout = (LinearLayout) activity.findViewById(R.id.positionLayout);
    }

    @Override
    public void initData(Activity activity) {
        getExtra(activity);

        title.setText(batch);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String result = checked();
                if (TextUtils.isEmpty(result)) {
                    AllocationUtils.savePositionListInMat(activity, mat_id, batch, positionBeanList);
                    finish();
                } else {
                    DialogUtils.getConfirmDialog(activity, result, new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            AllocationUtils.savePositionListInMat(AllocationInPositionsActivity.this, mat_id, batch, positionBeanList);
                            dialog.dismiss();
                            finish();
                        }
                    }).show();
                }
            }
        });

        itemListBean = AllocationUtils.getItemListBeanByMatId(activity, mat_id);
        positionBeanList = AllocationUtils.getPositionsByMatIdBatch(activity, mat_id, batch);

        for (int i = 0; i < positionBeanList.size(); i++) {
            addPosition(positionBeanList.get(i), positionBeanList.get(i).getNumber(), i);
        }
        id = positionBeanList.size();

        search.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!TextUtils.isEmpty(editText.getText().toString())) {
                    if (!AllocationUtils.havePosition(positionBeanList, editText.getText().toString())) {
                        checkPosition(activity, editText.getText().toString());
                    } else {
                        Utils.middleToast(activity, "该仓位已经存在");
                    }
                }
                Utils.closeSoftKeyboard(activity, v);
            }
        });

        setmScanListener((String scanResult, int length) -> {

            switch (QRCodeUtils.getQRCodeType(scanResult)) {
                case QRCodeConstant.CY_TYPE_POSITION:
                    checkPosition(this, scanResult);
                    break;
                default:
                    break;
            }
        }, QRCodeConstant.CY_TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mat_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.MAT_ID);
        batch = activity.getIntent().getStringExtra(AllocationInStorageExtra.BATCH);
    }

    private void addPosition(AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position positionBean, String count, int view_id) {
        AllocationUtils.createEditText(this, positionLayout, positionBean.getPosition_code(), count, itemListBean.getUnit_code(), view_id, new PositionsViewListener() {
            @Override
            public void deleteClickListener(int id, String positionCode) {
                for (int i = 0; i < positionBeanList.size(); i++) {
                    if (positionBeanList.get(i).getPosition_code().equals(positionCode)) {
                        positionBeanList.remove(i);
                        positionLayout.removeView(findViewById(id));
                        break;
                    }
                }
            }
        });
    }

    private void checkPosition(Context context, String position) {
        AllocationGetSNsByPackageRequest.request(context, position, itemListBean.getLocation_id(), new GsonBeanListener<AllocationGetSNsByPackageBean>() {
            @Override
            public void onSuccess(AllocationGetSNsByPackageBean bean) {
                if (bean.getBody() == null) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                    return;
                }
                if (bean.getBody().getType().equals("3")) {
                    AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position position = new AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean.Position();
                    position.setArea_id(bean.getBody().getArea_id());
                    position.setNumber("0");
                    position.setPosition_code(bean.getBody().getPosition_code());
                    position.setPosition_id(bean.getBody().getPosition_id());
                    position.setWh_id(bean.getBody().getWh_id());
                    position.setPositionViewId(id);
                    positionBeanList.add(position);
                    addPosition(position, "0", id);
                    id += 1;
                }
            }
        });
    }

    public interface PositionsViewListener {
        void deleteClickListener(int id, String positionCode);
    }

    @Override
    public void onBackPressed() {
        String result = checked();
        if (TextUtils.isEmpty(result)) {
            AllocationUtils.savePositionListInMat(this, mat_id, batch, positionBeanList);
            finish();
        } else {
            DialogUtils.getConfirmDialog(this, result, new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    AllocationUtils.savePositionListInMat(AllocationInPositionsActivity.this, mat_id, batch, positionBeanList);
                    dialog.dismiss();
                    finish();
                }
            }).show();
        }
    }

    private String checked() {
        String result = null;
        float count = 0;
        for (int i = 0; i < positionBeanList.size(); i++) {
            View view = findViewById(positionBeanList.get(i).getPositionViewId());//查询仓位存放id的list中的仓位
            EditText editText = (EditText) view.findViewById(R.id.text);
            count += TextUtils.isEmpty(editText.getText().toString()) ? 0 : Float.parseFloat(editText.getText().toString());
            positionBeanList.get(i).setNumber(TextUtils.isEmpty(editText.getText().toString()) ? "0" : editText.getText().toString());
        }
        if (count != Float.parseFloat(itemListBean.getQty())) {
            return "出库数量与入库数量不相等,确认退出?";
        }
        return result;
    }

}
